package com.ssm.changeunit.persistance;

import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.sql.*;
import java.util.Calendar;
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;

/**
 *
 * @author 시리우스
 */
public class ManagedChangeUnitDAO extends AbstractManagedChangeUnitDAO {
    @Override
    public ManagedChangeUnitDTO find() throws PersistanceException {
        ManagedChangeUnitDTO dto = null;
        
        // 검색 루틴 구현
        Connection conn = null; 
        Statement stmt = null; 
        ResultSet rs = null;
        
        //Create SQL Statement 
        String sql="select ManagedChangeUnitId "
                + " from APP.ManagedChangeUnit";
        
        try {
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            rs = stmt.executeQuery(sql);
            
            if(rs.next()) {
                dto = new ManagedChangeUnitDTO();
                
                dto.setManagedChangeUnitId(rs.getString("ManagedChangeUnitId"));
            }
        }
        catch(SQLException e) { 
            throw new PersistanceException("Exception while executing ManagedChangeUnitDAO.find: "+ e, e);
        } 
        finally {
            if(rs != null) 
                try {rs.close(); } 
                catch(Exception ignore) { } 
            if(stmt != null) 
                try { stmt.close(); }
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
        // 검색 결과를 리턴 
        return dto; 
    }

    @Override
    public void insert(ManagedChangeUnitDTO managedChangeUnitDTO) throws PersistanceException {
        Connection conn = null; 
        PreparedStatement stmt = null;  
        
        //Create SQL Statement 
        String sql="insert into APP.ManagedChangeUnit (ManagedChangeUnitId) " 
                    + "values (?)";
        
        System.out.println(sql);
        
        try { 
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, managedChangeUnitDTO.getManagedChangeUnitId());
            
            stmt.executeUpdate();
        }
        catch(SQLException e) {
            throw new PersistanceException("Exception while executing ManagedChangeUnit.insert: "+ e, e); 
        } 
        finally {
            if(stmt != null) 
                try { stmt.close(); } 
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        }
    }

    @Override
    public void update(ManagedChangeUnitDTO managedChangeUnitDTO) throws PersistanceException {
        Connection conn = null; 
        PreparedStatement stmt = null;  
        
        //Create SQL Statement 
        String sql="update APP.ManagedChangeUnit set ManagedChangeUnitId=?";
        
        try { 
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, managedChangeUnitDTO.getManagedChangeUnitId());
            
            stmt.executeUpdate();
        }
        catch(SQLException e) {
            throw new PersistanceException("Exception while executing ManagedChangeUnitDAO.update: "+ e, e); 
        } 
        finally {
            if(stmt != null) 
                try { stmt.close(); } 
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        }  
    }

    @Override
    public void delete() throws PersistanceException {
        Connection conn = null; 
        Statement stmt = null;  
        
        //Create SQL Statement 
        String sql="delete from APP.ManagedChangeUnit";  
        
        try { 
            conn = dm.getConnection(); 
            conn.setAutoCommit(true);   
            stmt = conn.createStatement(); 
            stmt.executeUpdate(sql); 
        }
        catch(SQLException e) {
            throw new PersistanceException("Exception while executing ManagedChangeUnitDAO.delete: "+ e, e); 
        } 
        finally {
            if(stmt != null) 
                try { stmt.close(); } 
                catch(Exception ignore) { }  
            if(conn != null) 
                try { conn.close(); } 
                catch(Exception ignore) { } 
        } 
    }
}